﻿using CPPEI.Coolzon.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.Designer.Views
{
    public class AreasInSceneRemoveOperation : OperationBase
    {
        private DesignView _designView;
        private AreaPart _removedArea;
        private int _oldIndex = -1;
        private Designtime.Metadata.NodeBase DT_MetadataForRemoved = null;
        /// <summary>
        /// 组件, oldLeft, newLeft
        /// </summary>
        private List<Tuple<Coolzon.UI.Part, double, double>> _areas;
        public AreasInSceneRemoveOperation(DesignView designView, Coolzon.UI.ScenePart scene, Coolzon.UI.AreaPart removedArea)
        {
            _designView = designView;
            _removedArea = removedArea;
            _areas = new List<Tuple<Part, double, double>>();

            string curAreaName = removedArea.Name;
            Coolzon.UI.AreaPart rightArea = scene.GetRightRootArea(removedArea);
            while (!string.Equals(rightArea.Name, curAreaName))
            {
                Tuple<Part, double, double> tmpArea = new Tuple<Part, double, double>(rightArea, rightArea.XLeft, rightArea.XLeft - removedArea.XWidth);
                _areas.Add(tmpArea);

                curAreaName = rightArea.Name;
                rightArea = scene.GetRightRootArea(rightArea);
            }
        }
        protected override void OnDo()
        {
            Designtime.Metadata.NodeBase nodeBase = null;
            Designtime.Metadata.Scene scene = Designtime.Metadata.SolutionHelper.GetScene(UIContext.Current.CurrentSolution, UIContext.Current.CurrentEditedNode.ID);
            nodeBase = Designtime.Metadata.SolutionHelper.GetArea(UIContext.Current.CurrentSolution, scene.Name, _removedArea.XFullName);
            DT_MetadataForRemoved = nodeBase;

            _oldIndex = (_removedArea.ParentPart as IPartContainer).GetPartIndex(_removedArea);
            _designView.API_Remove(_removedArea);
            foreach (var area in _areas)
            {
                try
                {
                    area.Item1.XLeft = area.Item3;
                    Coolzon.UI.XProperty.XPropertyRTUtil.SetPropertyValue(area.Item1.Metadata, "Left", typeof(double), area.Item3);
                }
                catch (Exception ex)
                {
                    Common.Logger.ErrorToTag(Library.LogTAG, "更新场景内区域{0}大小宽高属性出错,ex:{1}", area.Item1.Name, ex);
                }
            }
        }

        protected override void OnUndo()
        {
            double left = _removedArea.XLeft;

            _designView.API_AddPart(_removedArea, (IPartContainer)_removedArea.ParentPart, new System.Windows.Point(_removedArea.XLeft, _removedArea.XTop), new System.Windows.Size(_removedArea.XWidth, _removedArea.XHeight), _removedArea.Metadata.Type, DT_MetadataForRemoved);

            _removedArea.XLeft = left;
            Coolzon.UI.XProperty.XPropertyRTUtil.SetPropertyValue(_removedArea.Metadata, "Left", typeof(double), left);

            foreach (var area in _areas)
            {
                try
                {
                    area.Item1.XLeft = area.Item2;
                    Coolzon.UI.XProperty.XPropertyRTUtil.SetPropertyValue(area.Item1.Metadata, "Left", typeof(double), area.Item2);
                }
                catch (Exception ex)
                {
                    Common.Logger.ErrorToTag(Library.LogTAG, "更新场景内区域{0}大小宽高属性出错,ex:{1}", area.Item1.Name, ex);
                }
            }
            if (_oldIndex > -1)
            {
                (_removedArea.ParentPart as IPartContainer).BringPartTo(_removedArea, _oldIndex);
                Designtime.Metadata.Scene scene = Designtime.Metadata.SolutionHelper.GetScene(UIContext.Current.CurrentSolution, UIContext.Current.CurrentEditedNode.ID);
                Designtime.Metadata.Area area = Designtime.Metadata.SolutionHelper.GetArea(UIContext.Current.CurrentSolution, scene.Name, _removedArea.XFullName);
                scene.BringPartTo(area, _oldIndex);
                Event.UI.UIEventBus.Publish(new Event.UI.UIChangePartZIndex(_removedArea.XFullName, _oldIndex));
            }
        }
    }
}
